﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleLookUpForm : ChurchForm
    {
        #region Constructor & Load
        public PeopleLookUpForm()
        {
            InitializeComponent();
            this.Title = "Look Up";
        }

        private void PeopleLookUpForm_Load(object sender, EventArgs e)
        {
            List<PeopleSearchResults> lstResults = new List<PeopleSearchResults>();
            grdSearch.DataSource = lstResults;
            grdSearch.Columns["PeoplePK"].HeaderText = "Entry #";
        }

        #endregion Constructor & Load

        public void SetFocus()
        {
            this.txtFirstName.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtFirstName.Text.Trim().Length == 0 && this.txtLastName.Text.Trim().Length == 0
                && this.txtEmail.Text.Trim().Length == 0 && this.txtPhone.Text.Trim().Length == 0)
            {
                MessageBox.Show("You just gotta give me something to work with!", "No Search Criteria Provided");
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            this.lblSearching.Visible = true;
            this.Refresh();
            PeopleBO pBO = new PeopleBO();
            List<PeopleSearchResults> results = pBO.LookupPeople(this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), this.txtEmail.Text.Trim(), this.txtPhone.Text.Trim());
            this.grdSearch.DataSource = results;
            this.lblSearching.Visible = false;
            this.Refresh();
            Cursor.Current = Cursors.Default;
        }

        private void btnMakeNew_Click(object sender, EventArgs e)
        {
            AddDialog dlg = new AddDialog(this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim());
            dlg.ShowDialog();

            if (dlg.DialogResult == DialogResult.OK)
                ((MainForm)this.MdiParent).OpenPeopleMain(dlg.NewPeoplePK);
        }

        private void grdSearch_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (this.grdSearch.SelectedRows.Count > 0)
                ((MainForm)this.MdiParent).OpenPeopleMain((int)this.grdSearch.SelectedRows[0].Cells["PeoplePK"].Value);
        }

        private void txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.btnSearch.PerformClick();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtFirstName.Text = "";
            this.txtLastName.Text = "";
            this.txtEmail.Text = "";
            this.txtPhone.Text = "";
        }
    }
}
